/*
 * Copyright (C) 2015  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#ifndef PLANET_WARS_2D_RT_SDL2_SDL_H
#define PLANET_WARS_2D_RT_SDL2_SDL_H


#include <SDL2/SDL.h>
#include "PlanetWars2dRT-core/utils/bool.h"
#include "PlanetWars2dRT-core/utils/c_language.h"
#include <stdio.h>


#define DEFAULT_WINDOW_WIDTH  700
#define DEFAULT_WINDOW_HEIGHT 560


EXTERN_C_BEGIN


static inline
void
SDL_print_error()
{
  fputs("SDL_Error: ",  stderr);
  fputs(SDL_GetError(), stderr);
  fputc('\n',           stderr);
}

static inline
bool
SDL_init()
{
  if(SDL_Init(SDL_INIT_VIDEO) < 0)
    {
      fputs("SDL could not initialize!", stderr);
      fputc('\n', stderr);
      SDL_print_error();
      return false;
    }
  return true;
}

static inline
SDL_Window*
SDL_main_window_init()
{
  SDL_Window* window;
  window = SDL_CreateWindow("Planet Wars 2D RT",
			    SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
			    DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT,
			    SDL_WINDOW_SHOWN);
  if(window == NULL)
    {
      fprintf(stderr, "SDL could not create a window!\n");
      SDL_print_error();
    }
  return window;
}

static inline
SDL_Window*
SDL_and_main_window_init()
{
  return SDL_init() ? SDL_main_window_init() : NULL;
}

static inline
void
SDL_and_main_window_quit(SDL_Window* main_window)
{
  SDL_DestroyWindow(main_window);
  SDL_Quit();
}


EXTERN_C_END


#endif
